<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace app\blog\controller;

use app\admin\model\UserModel;
use app\blog\model\BusinessModel;
use app\blog\model\StoreModel;
use App\Http\Model\Home\goods;
use cmf\controller\AdminBaseController;
use think\Db;
class ShopinfoController extends AdminBaseController
{
    public function index(){

       $cat= DB::name('store')->field('cat_store')->select();
        //var_dump($cat);
        $this->assign('cat',$cat);
        return $this->fetch();
    }


    function handle(){
        //对shopinfo首页发送的post进行处理
        //

        if($this->request->isPost()){
            //header('Cache-control: private, must-revalidate');
            //session_cache_limiter('private, must-revalidate');
            $data=$this->request->Post();
            //var_dump($data['catstore']);exit;
            $user=DB::name('store')->where('cat_store',$data['cat_store'])->select();//判断数据库中是否有此店铺类别
            $store_name=DB::name('store')->where('store_name',$data['store_name'])->find();
            //var_dump($store_name);exit;
            $id1=DB::name('store')->where('cat_store',$data['cat_store'])->field('id')->find();
            $id=join('',$id1);
           $sn=DB::name('store')->where('cat_store',$data['cat_store'])->field('store_name')->select();
            //$sn1=join('',$sn);
            //var_dump($sn[0]['store_name']);exit;
            //var_dump($id);exit;
           //var_dump($user);
                    if(!empty($data['store_name'])){
                        if(!empty($data['principal'])){
                            if(!empty($data['mobile'])){
                                if(!empty($data['phone'])){
                                    if(!empty($data['address'])){
                                        if(!empty($data['operator'])){
                                            if(!empty($data['store_name'])){
                                                $data['ctime']=time();
                                                $data['jtime']=time();

                                                if($store_name){
                                                    //判断数据库中是否有此商铺名称，有的话
                                                    /*if(!$user){

                                                        $uu=new StoreModel();
                                                        $uu->where('id',$id)->setField($data);
                                                        echo "<script>alert('更新成功')</script>";
                                                    }else{
                                                        $uu=new StoreModel();
                                                        $uu->where('id',$id)->setField($data);
                                                        echo "<script>alert('提交成功')</script>";
                                                    }*/
                                                    echo "<script>alert('已有此商家，请改名')</script>";
                                                    $this->error('重新写吧，少年', url('shopinfo/index'));

                                                }else{
                                                    //没有店铺名称的话
                                                    if($user && empty($sn[0]['store_name'])){
                                                        // 如果数据库有此店铺类型的话，并且商店类型是空的时候，更新数据库
                                                        $uu=new StoreModel();
                                                        $uu->where('id',$id)->setField($data);
                                                        echo "<script>alert('更新成功')</script>";
                                                    }else{

                                                            $abc=DB::name('store_type')->where('cat_name',$data['cat_store'])->select();
                                                            //var_dump($abc[0]['cat_goods']);exit;
                                                            $bcd=DB::name('business')->where('type',$abc[0]['cat_goods'])->select();
                                                            //var_dump($bcd[0]['goods']);exit;//套餐商品
                                                            $data['cat_goods']=$abc[0]['cat_goods'];
                                                            $data['franchise_fee']=$abc[0]['franchise_fee'];
                                                            $data['cat_ware']=$bcd[0]['goods'];

                                                            $userr=new StoreModel();
                                                            $userr->save($data);
                                                            //echo "<script>alert('插入新商家成功')</script>";
                                                        $this->success('插入新商家成功',url('shopdetail/index'));


                                                    }

                                                }
                                            }else{
                                                echo "<script>alert('店铺类别不能为空')</script>";
                                                $this->success('重新写吧，少年', url('shopinfo/index'));
                                            }


                                        }else{
                                            echo "<script>alert('负责人不能为空')</script>";
                                            $this->success('重新写吧，少年', url('shopinfo/index'));
                                        }

                                    }else{
                                        echo "<script>alert('店铺地址不能为空')</script>";
                                        $this->success('重新写吧，少年', url('shopinfo/index'));
                                    }

                                }else{
                                    echo "<script>alert('店铺电话不能为空')</script>";
                                    $this->success('重新写吧，少年', url('shopinfo/index'));
                                }

                            }else{
                                echo "<script>alert('负责人手机不能为空')</script>";
                                $this->success('重新写吧，少年', url('shopinfo/index'));
                            }


                        }else{
                            echo "<script>alert('负责人不能为空')</script>";
                            $this->success('重新写吧，少年', url('shopinfo/index'));
                            //return back('shopinfo/index');
                        }
                    }else{
                        echo "<script>alert('店铺名称不能为空')</script>";
                        $this->success('重新写吧，少年', url('shopinfo/index'));

                    }

        }
    }


}
























